﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.CC.BL.Common;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for ContractInfoInput.
    /// </summary>
    public class ContractInfoInput : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox tbContractDate;
        protected System.Web.UI.WebControls.Label lbContractDateReadOnly;
        protected System.Web.UI.WebControls.TextBox tbContractNo;
        protected System.Web.UI.WebControls.Label lbContractNoReadOnly;
        protected System.Web.UI.WebControls.TextBox tbContractProducts;
        protected System.Web.UI.WebControls.Label lbContractProductsReadOnly;
        protected System.Web.UI.WebControls.TextBox tbContractTripPurpose;
        protected System.Web.UI.WebControls.Label lbContractTripPurposeReadOnly;
        protected System.Web.UI.WebControls.TextBox tbContractQuotas;
        protected System.Web.UI.WebControls.Label lbContractQuotasReadOnly;
        protected System.Web.UI.WebControls.TextBox tbUnitPrice;
        protected System.Web.UI.WebControls.Label lbUnitPriceReadOnly;
        protected System.Web.UI.WebControls.TextBox tbReservedBudget;
        protected System.Web.UI.WebControls.Label lbReservedBudgetReadOnly;
        protected GDC.Utility.Web.ImgRequiredFieldValidator ContractDateRequired;
        protected GDC.Utility.Web.ImgRequiredFieldValidator ContractQuotasRequired;
        protected GDC.Utility.Web.ImgRangeValidator ContractQuotasRange;
        protected GDC.Utility.Web.ImgRequiredFieldValidator UnitPriceRequired;
        protected GDC.Utility.Web.ImgRangeValidator UnitPriceRange;
        protected GDC.Utility.Web.ImgRequiredFieldValidator ReservedBudgetRequired;
        protected GDC.Utility.Web.ImgRangeValidator ReservedBudgetRange;
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.ImageButton Btn_Delete;
        protected System.Web.UI.WebControls.Button Btn_Close;
        protected System.Web.UI.HtmlControls.HtmlGenericControl img1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            this.RowID = Convert.ToInt32(Request["ContractInfoId"]);
            if (!this.IsPostBack)
            {
                this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";

                if (this.RowID >= 0)
                {
                    InitForm();
                }
            }
            this.tbContractQuotas.Attributes["onchange"] = "javascript:OnQuotaChange();";
            this.tbUnitPrice.Attributes["onchange"] = "javascript:OnPriceChange();";
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            this.Btn_Close.Click += new EventHandler(Btn_Close_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        void Btn_Close_Click(object sender, EventArgs e)
        {
            Response.Write(@"<Script>returnValue='%close%';self.close(); </Script>");
        }

        void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateContractInfoSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }

        private void UpdateContractInfoSession()
        {
            DataTable contractInfo = null;
            DataRow contractRow = null;
            object SessContractInfo = Session[SessionFlag.ContractInfo];
            if (SessContractInfo != null)
            {
                contractInfo = (DataTable)SessContractInfo;
            }
            else
            {
                return;
            }

            if (this.RowID == 0)
            {
                contractRow = contractInfo.NewRow();
                contractRow["DateOfContract"] = this.tbContractDate.Text;
                contractRow["ContractNo"] = this.tbContractNo.Text;
                contractRow["ProducstSold"] = this.tbContractProducts.Text;
                contractRow["VisitPurposeInContract"] = this.tbContractTripPurpose.Text;
                contractRow["Quotas"] = this.tbContractQuotas.Text == string.Empty ? "0" : this.tbContractQuotas.Text;
                contractRow["UnitPrice"] = this.tbUnitPrice.Text == string.Empty ? "0" : this.tbUnitPrice.Text;
                contractRow["Budget"] = this.tbReservedBudget.Text;
                contractInfo.Rows.Add(contractRow);
            }
            else if (this.RowID >= 0)
            {
                contractRow = contractInfo.Rows[this.RowID - 1];
                contractRow["DateOfContract"] = this.tbContractDate.Text;
                contractRow["ContractNo"] = this.tbContractNo.Text;
                contractRow["ProducstSold"] = this.tbContractProducts.Text;
                contractRow["VisitPurposeInContract"] = this.tbContractTripPurpose.Text;
                contractRow["Quotas"] = this.tbContractQuotas.Text == string.Empty ? "0" : this.tbContractQuotas.Text;
                contractRow["UnitPrice"] = this.tbUnitPrice.Text == string.Empty ? "0" : this.tbUnitPrice.Text;
                contractRow["Budget"] = this.tbReservedBudget.Text;
            }

            Session[SessionFlag.ContractInfo] = SetRowID(contractInfo);
        }

        private void InitForm()
        {
            DataTable contractInfo = null;
            DataRow contractRow = null;
            object sessContractInfo = Session[SessionFlag.ContractInfo];
            if (sessContractInfo != null)
            {
                contractInfo = (DataTable)sessContractInfo;
            }
            else
            {
                return;
            }

            if (this.RowID > 0)
            {
                contractRow = contractInfo.Rows[this.RowID - 1];

                this.tbContractDate.Text = Convert.ToDateTime(contractRow["DateOfContract"]).ToString("yyyy/MM/dd");
                this.tbContractNo.Text = contractRow["ContractNo"].ToString();
                this.tbContractProducts.Text = contractRow["ProducstSold"].ToString();
                this.tbContractTripPurpose.Text = contractRow["VisitPurposeInContract"].ToString();
                this.tbContractQuotas.Text = contractRow["Quotas"].ToString();
                this.tbUnitPrice.Text = contractRow["UnitPrice"].ToString();
                this.tbReservedBudget.Text = contractRow["Budget"].ToString();
            }
        }

        #region RowID State
        protected int RowID
        {
            get { return (int)this.ViewState["AccatelAttendeeRowID"]; }
            set { this.ViewState["AccatelAttendeeRowID"] = value; }
        }
        #endregion

        private DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1]["RowId"] = i.ToString();
                }
            return table;
        }
    }
}
